
// package com.cskaoyan.config;
//
// import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
// import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
// import org.springframework.context.annotation.Bean;
// import org.springframework.context.annotation.Configuration;
//
// /**
//  * 创建日期: 2022/05/09 11:00
//  *
//  * @author liutao
//  */
// @Configuration
// public class ShiroConfiguration {
//     // ShiroFilter
//     @Bean
//     public ShiroFilterFactoryBean shiroFilter(DefaultWebSecurityManager securityManager) {
//         ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
//         shiroFilterFactoryBean.setSecurityManager(securityManager);
//         return shiroFilterFactoryBean;
//     }
//
//     // SecurityManager
//     @Bean
//     public DefaultWebSecurityManager securityManager(CustomRealm customRealm) {
//         DefaultWebSecurityManager defaultWebSecurityManager = new DefaultWebSecurityManager();
//         defaultWebSecurityManager.setRealm(customRealm);
//         return defaultWebSecurityManager;
//     }
//
//     // Realm -> 获得认证信息和授权信息
//     @Bean
//     public CustomRealm customRealm() {
//         return new CustomRealm();
//     }
//
//     // SessionManager
//
// }

package com.cskaoyan.config;

import org.apache.catalina.Manager;
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.LinkedHashMap;

/**
 * 创建日期: 2022/05/09 16:04
 *
 * @author 孙豪骏
 */
@Configuration
public class ShiroConfiguration {

    // shiroFilter
    @Bean
    public ShiroFilterFactoryBean shiroFilter(DefaultWebSecurityManager securityManager) {
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        shiroFilterFactoryBean.setSecurityManager(securityManager);
        LinkedHashMap<String, String> filterChainDefinitionMap = new LinkedHashMap<>();
        // 用这个filterchaimap去设置过滤器
        // todo 还没有配置 filterChainDefinitionMap.put();
        // anon 匿名不需要认证和授权
        filterChainDefinitionMap.put("/admin/auth/login", "anon");
        filterChainDefinitionMap.put("/admin/auth/info", "anon");
        filterChainDefinitionMap.put("/admin/config/wx", "anon");

//        // admin开头的请求，都要通过认证Filter
        filterChainDefinitionMap.put("admin/role/permissions","perms");



        filterChainDefinitionMap.put("/wx/auth/login", "anon");
        filterChainDefinitionMap.put("/wx/home/**", "anon");
        filterChainDefinitionMap.put("/wx/goods/**", "anon");
        filterChainDefinitionMap.put("/wx/brand/**", "anon");
        filterChainDefinitionMap.put("/wx/catalog/**", "anon");
        filterChainDefinitionMap.put("/wx/search/**", "anon");
        filterChainDefinitionMap.put("/wx/topic/**", "anon");
        filterChainDefinitionMap.put("/wx/comment/**", "anon");
        filterChainDefinitionMap.put("/wx/coupon/list", "anon");
        filterChainDefinitionMap.put("/wx/cart/**", "anon");
        filterChainDefinitionMap.put("/wx/feedback/**", "anon");

        // admin、wx开头的请求，都要通过认证Filter
        filterChainDefinitionMap.put("/wx/**", "authc");
        filterChainDefinitionMap.put("/admin/**", "authc");

        filterChainDefinitionMap.put("/admin/admin/list", "perms[admin:admin:list]");

        shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);
        return shiroFilterFactoryBean;
    }


    // Realm -> 获得认证信息和授权信息
    // @Bean
    // public CustomRealm customRealm() {
    //     return new CustomRealm();
    // }


    // 用到AspectJ → 使用注解的方式，将权限和url绑定起来
    @Bean
    public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(DefaultWebSecurityManager securityManager) {
        AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor = new AuthorizationAttributeSourceAdvisor();
        authorizationAttributeSourceAdvisor.setSecurityManager(securityManager);
        return authorizationAttributeSourceAdvisor;
    }





    // Securit yManager
    @Bean
    public DefaultWebSecurityManager securityManager(CustomRealm customRealm, CustomShiroSessionManager customShiroSessionManager) {
        DefaultWebSecurityManager defaultWebSecurityManager = new DefaultWebSecurityManager();
        defaultWebSecurityManager.setRealm(customRealm);
        defaultWebSecurityManager.setSessionManager(customShiroSessionManager);
        return defaultWebSecurityManager;
    }

    //Realm,有默认提供，自定义realm 再realm类里面


}

